package com.freejava.myblog.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

@Configuration
public class AuthInterceptorRegister implements WebMvcConfigurer {
    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        registry
                // 指定拦截器
                .addInterceptor(authenticationInterceptor())
                // 配置拦截器的拦截路径，/**指的是拦截所有的请求
                .addPathPatterns("/**")
        // 对登录和注册接口放行，不拦截和拦截的接口可能会调整
        // 这里配置的话不是很方便
//                .excludePathPatterns(Arrays.asList("/login", "/register"))
        ;
    }

    @Bean
    public AuthInterceptor authenticationInterceptor() {
        return new AuthInterceptor();
    }
}